﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Repository;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Areas.Management.Controllers
{
    public class AccountListController : Controller
    {
        // Create an instance of user manage
        LeadVinaglue.Repository.UserManagementRepository repository = new LeadVinaglue.Repository.UserManagementRepository();

        //
        // GET: /Management/Home/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", "ClientAdmin", string.Empty, "CreationDate", "desc");
            return View(model);
        }
        
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult GetData(string keywords, int pageSize, int pageIndex, string sortBy, string sortOrder, int id)
        {
            if (keywords.Contains("Keywords"))
                keywords = string.Empty;

            // First: Update Locked/Unlock users
            Repository.UserRepository userRepository = new Repository.UserRepository();
            LeadVinaglue.User user = new LeadVinaglue.User();
            user = userRepository.GetSingle(id);
            if (user != null)
            {
                user.IsApproved = true;
                user.Active = true;
                user.IsLockedOut = (user.IsLockedOut.Value) ? false : true;
                userRepository.SaveIsLockedOut(user);
            }

            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel("ClientAdmin", keywords, sortBy, sortOrder, pageSize, pageIndex);


            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult View(int id)
        {
            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel("Menu", "SuperAdministrator", id);

            ViewBag.UserId = id;

            return View(model);
        }

        // GET: Reports Account
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Report()
        {
            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", "ClientAdmin", string.Empty, "CreationDate", "desc");
            return View(model);
        }

        public FileResult ExportToExcel()
        {
            LeadVinaglue.Repository.UserManagementRepository repository = new LeadVinaglue.Repository.UserManagementRepository();
            FileResult fileReport = repository.ExportToCSVFile();

            return fileReport;
        }

        #region Transaction
        [HttpPost]
        public ActionResult UpdateTransaction(int userId, int id, int total, bool visible, bool vip)
        {
            // Create an instance of transaction
            UserTransaction ut = new UserTransaction();
            UserTransactionRepository utRepository = new UserTransactionRepository();

            if (id > 0)
            {
                ut = utRepository.GetSingle(id);
                ut.Total = total;
                ut.Visible = visible;
                ut.TransactionDate = DateTime.Now;
                
                utRepository.Save(ut);
            }
            else
            {
                ut.UserId = userId;
                ut.Total = total;
                ut.Visible = visible;
                ut.GUID = Guid.NewGuid().ToString();
                ut.TransactionDate = DateTime.Now;
                ut.TotalPoint = total * 1000;
                ut.DateCreated = DateTime.Now;
                utRepository.Create(ut);
            }

            // Model
            UserManagementModel model = new UserManagementModel();
            model.UserTransaction = utRepository.GetAll(userId);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult DeleteTransaction(int userId, int id)
        {
            // Create an instance of transaction
            UserTransactionRepository utRepository = new UserTransactionRepository();
            utRepository.Delete(id);

            // Model
            UserManagementModel model = new UserManagementModel();
            model.UserTransaction = utRepository.GetAll(userId);

            return Json(model, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
